package com.yidu.hbatsams.service.TrainingPlan.zjj;

import com.yidu.hbatsams.dao.AtsPlanWeekMapper;
import com.yidu.hbatsams.domain.AtsEmpAthleteScheme;
import com.yidu.hbatsams.domain.AtsPlanWeek;
import com.yidu.hbatsams.service.TrainingPlan.AtsEmpAthleteSchemeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * @author 朱星润
 * @类的描述——
 * @date 2019/10/30下午 04:02
 */
@Service
public class PlanWeekServiceImpl implements PlanWeekService {
    //运动员和计划的接口
    @Resource
    AtsEmpAthleteSchemeService atsEmpAthleteSchemeService;
    @Resource
    AtsPlanWeekMapper atsPlanWeekMapper;

    @Override
    public void insertSelectiveFour(AtsPlanWeek record) {
        atsPlanWeekMapper.insertSelectiveFour(record);
        if(record.getAthleteId()!=""&&!record.getAthleteName().equals("")){
            //new出计划教练员关联表
            AtsEmpAthleteScheme atsEmpAthlete=new AtsEmpAthleteScheme();
            atsEmpAthlete.setCreater(record.getCreater());
            //传入2代表周训练计划
            atsEmpAthlete.setRemark("2");
            atsEmpAthlete.setPname(record.getWeekName());
            atsEmpAthlete.setPid(record.getWeekId());
            boolean flag=record.getAthleteId().contains(",");
            if(flag==true) {
                String[] athleteId = record.getAthleteId().split(",");
                String[] athleteName = record.getAthleteName().split(",");
                for (int i = 0; i < athleteId.length; i++) {
                    String athleteIdone = athleteId[i];
                    String athleteNameone = athleteName[i];
                    atsEmpAthlete.setAthleteId(Integer.valueOf(athleteIdone));
                    atsEmpAthlete.setAthleteName(athleteNameone);
                    atsEmpAthleteSchemeService.insertSelective(atsEmpAthlete);
                }
            }else{
                atsEmpAthlete.setAthleteId(Integer.valueOf(record.getAthleteId()));
                atsEmpAthlete.setAthleteName(record.getAthleteName());
                atsEmpAthleteSchemeService.insertSelective(atsEmpAthlete);
            }
        }
    }

    @Override
    public int updateByPrimaryKeySelectiveFour(AtsPlanWeek planWeek) {

        Date utilDate = new Date();//util.Date
        java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
        //传入创建时间
        planWeek.setCreatTime(sqlDate);
        return atsPlanWeekMapper.updateByPrimaryKeySelectiveFour(planWeek);
    }

    @Override
    public void update(AtsPlanWeek record) {
        AtsPlanWeek atsPlanWeek=new AtsPlanWeek();
        Date utilDate = new Date();//util.Date
        atsPlanWeek.setSportId(record.getSportId());
        java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
        record.setCreatTime(sqlDate);
        atsPlanWeekMapper.updateByPrimaryKeySelectiveFour(atsPlanWeek);
        AtsEmpAthleteScheme atsEmpAthleteScheme=new AtsEmpAthleteScheme();
        atsEmpAthleteScheme.setIsva(0);
        atsEmpAthleteScheme.setPid(record.getWeekId());
        atsEmpAthleteScheme.setRemark("2");//周计划
        int rowsone=atsEmpAthleteSchemeService.updateRemark(atsEmpAthleteScheme);
        if(rowsone>0){
            //new出计划教练员关联表
            AtsEmpAthleteScheme atsEmpAthlete=new AtsEmpAthleteScheme();
            atsEmpAthlete.setCreater(record.getCreater());
            //传入2代表周训练计划
            atsEmpAthlete.setRemark("2");
            atsEmpAthlete.setPname(record.getWeekName());
            atsEmpAthlete.setPid(record.getWeekId());
            boolean flag=record.getAthleteId().contains(",");
            if(flag==true) {
                String[] athleteId = record.getAthleteId().split(",");
                String[] athleteName = record.getAthleteName().split(",");
                for (int i = 0; i < athleteId.length; i++) {
                    String athleteIdone = athleteId[i];
                    String athleteNameone = athleteName[i];
                    atsEmpAthlete.setAthleteId(Integer.valueOf(athleteIdone));
                    atsEmpAthlete.setAthleteName(athleteNameone);
                    atsEmpAthleteSchemeService.insertSelective(atsEmpAthlete);
                }
            }else{
                atsEmpAthlete.setAthleteId(Integer.valueOf(record.getAthleteId()));
                atsEmpAthlete.setAthleteName(record.getAthleteName());
                atsEmpAthleteSchemeService.insertSelective(atsEmpAthlete);
            }
        }
    }

    @Override
    public AtsPlanWeek findByIdFour(Integer weekId) {
        return atsPlanWeekMapper.findById(weekId);
    }

    @Override
    public int countIsvaTwo(Integer coachId) {
        return atsPlanWeekMapper.countIsvaTwo(coachId);
    }

    @Override
    public int selectMaxIsvaTwo(Integer coachId) {
        return atsPlanWeekMapper.selectMaxIsvaTwo(coachId);
    }


    public boolean selectIsExists(AtsPlanWeek week) {


        List list = atsPlanWeekMapper.selectIsExists(week);
        return list.size()>0;
    }

}
